// pages/fenxiang/fenxiang.js
import Config from "../../common/config.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bgImgUrl: 'https://zhangyushu.liangziqishi.shop/images/haibao1.png',
    codeImgUrl: '',
    prurl: [],
    userId:"",
    success:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket:true
    });
    let obj = this
    wx.setNavigationBarTitle({
      title: '分享',
    })
    this.getCode()
    this.loadImg(this.data.bgImgUrl).then(data => {
      console.log(data);
      this.setData({
        bgImgUrl: data
      })
    });
    wx.request({
      url: Config.url+'/api/integralRule/share',
      method:"POST",
      header: {
        'content-type': 'application/json', //默认值
        'accessToken': wx.getStorageSync('sessionId')
      },
      success:function(res){
        console.log(res.data.data.userId);
        obj.setData({
          userId: res.data.data.userId
        });
     
        console.log(obj.data.userId);
      }
    }); 
    wx.setStorage({
      key: 'userId',
      data: obj.data.userId,
    })  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      return {
        title: '章鱼书',
        imageUrl: '../../images/yaoqing.png',
        path: 'pages/bookShelf/bookShelf?userId=' + this.data.userId,
        success: function () {
          console.log("转发成功")
        }
      }
    }
  },

  /**
   * 获取二维码
  */
  getCode () {
    let that = this
    wx.request({
      url: Config.url + '/api/activity/share',
      method: 'POST',
      header: {
        'content-type': 'application/json', //默认值
        'accessToken': wx.getStorageSync('sessionId')
      },
      success: function(data) {
        that.loadImg(data.data.data.data).then(data => {
          that.setData({
            codeImgUrl: data
          })
        })
      }
    })
  },

  /**
   * 生成朋友圈海报
  */
  createPoster: function () {
    var that = this
    if(!this.data.codeImgUrl) {
      wx.showToast({
        title: '正在生成小程序码，请稍后再试',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '努力生成中...',
      success: function (e) {
        console.log(e)
      }
    })

    //主要就是计算好各个图文的位置
    const ctx = wx.createCanvasContext('shareImg')
    ctx.save();

    //主要就是计算好各个图文的位
    ctx.setLineWidth(8);
    ctx.setStrokeStyle('#72AB9C')
    ctx.fillStyle = "#ffffff"
    ctx.fillRect(0, 0, 750, 1335)
    console.log(that.data.bgImgUrl)
    // wx.showModal({
    //   content: that.data.codeImgUrl
    // })
    ctx.drawImage(that.data.bgImgUrl, 36, 36, 678, 956);
    ctx.drawImage(that.data.codeImgUrl, 280, 1006, 187, 187);
    // ctx.arc(566, 228, 100, 2 * Math.PI, 0, false);

    showImg()
    function showImg() {
      return new Promise(function (resolve, reject) {
        // setTimeout(function () {
        ctx.stroke()
        ctx.draw(false, function () {
          wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: 750,
            height: 1334,
            destWidth: 1500,
            destHeight: 2668,
            canvasId: 'shareImg',
            success: function (res) {
              var ary = []
              ary.push(res.tempFilePath)
              console.log(ary)
              that.setData({
                prurl: ary,
                success:true
              })
              wx.hideLoading()
              wx.previewImage({
                urls: that.data.prurl // 需要预览的图片http链接列表  
              })
            },
            fail: function (res) {
              console.log(res)
            }
          })
        })
        resolve()
        // }, 100)
      })
    }

  },
  loadImg (imgUrl) {
    //缓存网络图片
    return new Promise((resolve, reject) => {
      wx.getImageInfo({
        src: imgUrl,
        success: function (response) {
          resolve(response.path)
        },
        fail: function (res) {
          reject()
        }
      })
    })
  }

})